import Vue from "vue";
import Router from "vue-router";

Vue.use(Router);

/* Layout */
import Layout from "@/layout";
import Layout2 from "@/layout2";

/**
 * 注意:子菜单只在路由子菜单时出现，children.length >= 1
 * 细节见: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   如果设置为true，项目将不会显示在侧栏(默认为false)
 * alwaysShow: true               如果设置为true, 始终显示根菜单
 *                                如果不设置alwaysShow, 当项目有多个孩子路径时，
 *                                它将成为嵌套模式，否则不显示根菜单
 * redirect: noRedirect           如果设置noRedirect 不会在面包屑breadcrumb中重定向
 * name:'router-name'             name用于<keep-alive>(必须设置!!!每个要不相同)
 * meta : {
    roles: ['ADMIN','editor']    控制页面角色(您可以设置多个角色)
    title: 'title'               名称显示在侧栏sidebar和面包屑breadcrumb(推荐设置)
    icon: 'svg-name'             图标显示在侧栏sidebar中
    breadcrumb: false            如果设置false, 项目将隐藏在breadcrumb(默认为true)
    activeMenu: '/example/list'  如果设置path, 侧栏sidebar将高亮突出显示您设置的路径
  }
 */

/**
 * constantRoutes
 * 没有权限要求的基本页
 * 可以访问所有角色
 */
// 所有权限通用路由表
// 如首页和登录页和一些不用权限的公用页面
export const constantRoutes = [
  {
    path: "/login",
    component: () => import("@/views/login/index"),
    hidden: true
  },
  {
    path: "/login-path",
    component: () => import("@/views/login/login-path/index"),
    hidden: true
  },
  { path: "/404", component: () => import("@/views/404"), hidden: true },
  {
    path: "/redirect",
    component: Layout,
    hidden: true,
    children: [
      {
        path: "/redirect/:path*",
        component: () => import("@/views/redirect/index")
      }
    ]
  }
  // , {
  //   path: '/icons',
  //   component: Layout,
  //   redirect: '/icons/index',
  //   children: [{
  //     path: '/icons/index',
  //     name: 'icons',
  //     component: () => import('@/views/icons/index'),
  //     meta: { title: '图标管理', icon: 'icon' }
  //   }]
  // }
];

// 异步挂载的路由
// 动态需要根据权限加载的路由表
export const asyncRoutes = [
  {
    path: "/",
    component: Layout,
    redirect: "/index",
    meta: { title: "首页", icon: "home", roles: ["ADMIN"] },
    children: [
      {
        path: "index",
        name: "index/index",
        component: () => import("@/views/home/index/index"),
        meta: { title: "首页", affix: true }
      }
    ]
  },
  {
    path: "/help-center",
    component: Layout,
    redirect: "/help-center/list",
    meta: { title: "帮助中心", icon: "sms", roles: ["ADMIN"] },
    children: [
      {
        path: "list",
        name: "help-center/list",
        component: () =>
          import("@/views/platform-setup/help-center/list/index"),
        meta: { title: "帮助中心" }
      }
    ]
  },
  {
    path: "/renovation-rights-protection",
    component: Layout,
    name: "renovation-rights-protection",
    redirect: "/renovation-rights-protection/list",
    meta: { title: "装修维权", icon: "hosting", roles: ["ADMIN"] },
    children: [
      {
        path: "list",
        name: "renovation-rights-protection/list",
        component: () => import("@/views/renovation-rights-protection/list"),
        meta: { title: "装修维权" }
      }
    ]
  },
  {
    path: "/my-info",
    component: Layout,
    redirect: "/my-info/index",
    name: "my-info",
    meta: { title: "我的信息", roles: ["ADMIN"] },
    children: [
      {
        path: "index",
        name: "my-info/index",
        component: () => import("@/views/my-info/index"),
        meta: { title: "我的信息", icon: "user" }
      }
    ]
  },
  // 404 page must be placed at the end !!!
  { path: "*", redirect: "/404", hidden: true }
];

const createRouter = () =>
  new Router({
    // mode: 'history', // 需要服务支持
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes
  });

const router = createRouter();
// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}

export default router;
